package com.zbkj.crmeb.store.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crmeb.core.common.CommonResult;
import com.zbkj.crmeb.store.model.StoreSeasonMap;
import com.zbkj.crmeb.store.request.StoreSeasonMapRequest;
import com.zbkj.crmeb.store.service.StoreSeasonMapService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.Set;

/**
 * 季节映射管理
 *
 * @author cdm
 * @Description:
 * @date 2023/07/04
 */
@RestController
@RequestMapping("api/admin/store/seasonMap")
@Api(tags = "季节映射管理")
public class StoreSeasonMapController {
    @Resource
    StoreSeasonMapService storeSeasonMapService;

    /**
     * 列表
     *
     * @param @return
     * @return CommonResult<Object>
     * @throws
     * @Description:
     * @author cdm
     */
    @ApiOperation(value = "列表")
    @GetMapping("/list")
    public CommonResult<Object> listEntity() {
        return CommonResult.success(storeSeasonMapService.selectEntityList());
    }

    /**
     * 分页查询
     *
     * @param @param  storeSeasonMapRequest
     * @param @return
     * @return CommonResult<Object>
     * @throws
     * @Description:
     * @author cdm
     */
    @ApiOperation(value = "分页")
    @PostMapping("/page")
    public CommonResult<Object> pageEntity(@RequestBody StoreSeasonMapRequest request) {
        return CommonResult.success(storeSeasonMapService.pageEntity(request));
    }

    /**
     * 通过id查询
     *
     * @param @param  id
     * @param @return
     * @return CommonResult<Object>
     * @throws
     * @Description:
     * @author cdm
     */
    @ApiOperation(value = "通过id获取")
    @GetMapping("/{id}")
    public CommonResult<Object> getEntity(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id) {
        return CommonResult.success(storeSeasonMapService.getEntityById(id));
    }

    /**
     * 新增
     *
     * @param @param  storeBrandDTO
     * @param @return
     * @return CommonResult<Object>
     * @throws
     * @Description:
     * @author cdm
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public CommonResult<Object> save(@RequestBody StoreSeasonMap request) {
        if (StrUtil.isEmpty(request.getName())) {
            return CommonResult.validateFailed("名称不能为空");
        }
        if (StrUtil.isEmpty(request.getReplaceName())) {
            return CommonResult.validateFailed("替换名称不能为空");
        }
        StoreSeasonMap storeSeasonMap = storeSeasonMapService.getOne(Wrappers.<StoreSeasonMap>lambdaQuery().eq(StoreSeasonMap::getName, request.getName()));
        if (null != storeSeasonMap) {
            return CommonResult.validateFailed("该名称已存在");
        }
        return 0 < storeSeasonMapService.saveEntity(request) ? CommonResult.success() : CommonResult.failed();
    }

    /**
     * 修改
     *
     * @param request
     * @return
     * @return CommonResult<Object>
     * @throws
     * @author cdm
     */
    @ApiOperation(value = "修改")
    @PutMapping("/update")
    public CommonResult<Object> update(@RequestBody StoreSeasonMap request) {
        if (null == request.getId()) {
            return CommonResult.validateFailed("ID不能为空");
        }
        if (StrUtil.isEmpty(request.getName())) {
            return CommonResult.validateFailed("名称不能为空");
        }
        if (StrUtil.isEmpty(request.getReplaceName())) {
            return CommonResult.validateFailed("替换名称不能为空");
        }
        StoreSeasonMap storeSeasonMap = storeSeasonMapService.getOne(Wrappers.<StoreSeasonMap>lambdaQuery().eq(StoreSeasonMap::getName, request.getName()));
        if (null != storeSeasonMap && ObjectUtil.notEqual(request.getId(), storeSeasonMap.getId())) {
            return CommonResult.validateFailed("该名称已存在");
        }
        return CommonResult.success(storeSeasonMapService.updateEntity(request));
    }

    /**
     * 修复
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "单个修复")
    @PutMapping("/repair/{id}")
    public CommonResult<Object> repair(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id) {
        return CommonResult.success(storeSeasonMapService.repair(id));
    }

    /**
     * 修复所有
     * @return
     */
    @ApiOperation(value = "修复所有")
    @PutMapping("/repairAll")
    public CommonResult<Object> repairAll() {
        return CommonResult.success(storeSeasonMapService.repairAll());
    }


    /**
     * 根据id删除
     *
     * @param @param  id
     * @param @return
     * @return CommonResult<Object>
     * @throws
     * @Description:
     * @author cdm
     */
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/{id}")
    public CommonResult<Object> del(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id) {
        return CommonResult.success(storeSeasonMapService.removeById(id));
    }

    /**
     * 批量删除
     *
     * @param @param  ids
     * @param @return
     * @return CommonResult<Object>
     * @throws
     * @Description:
     * @author daigb
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping("/remove")
    public CommonResult<Object> delBatch(@NotEmpty(message = "id不能为空") @RequestBody Set<Integer> ids) {
        return CommonResult.success(storeSeasonMapService.removeByIds(ids));
    }

}
